package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import controller.ActionList;
import controller.AddMenuMain;
import controller.FileDoc;
import controller.SearchAction;
import controller.SearchByGroup;
import controller.modifyAction;

import model.*;

/**
 * This Class represents the main window of the application
 * @author Estelle Latronico
 * 
 */

public class MainWindow{
	
	private Carnet CARNET = new Carnet("Estelle");
	private JFrame window = new JFrame();
	
	/**
	 * We use a stringBuildre to write each text/string in the class
	 */
	private StringBuilder buff = new StringBuilder();
	
	
	
	/**
	 * it is all the panel which is use in the main window
	 */
	private JPanel left = new JPanel();
	private JPanel right = new JPanel();
	private JPanel north = new JPanel();
	
	private JPanel menuright = new JPanel();
	private JPanel menuleft = new JPanel();
	private JPanel searchbycategorie = new JPanel();
	private JPanel showAll = new JPanel();
	
	/**
	 * the list of contact
	 */
	private JList<String> list;

	/**
	 * the JTextPane which allows to see the detail of a contact
	 */
	private JTextPane person = new JTextPane();
	
	private JButton modify = new JButton("Modify");
	private JTextField search = new JTextField("Search");
	
//--------------------------------------------------------------------------------------------------------------------------------------------------------		

//--------------------------------------------------------------------------------------------------------------------------------------------------------		

	public MainWindow(){
	
		/**
		 * We create a new object of fileDoc to extract all contacts from the Estelle's File.
		 * We copy it onto CARNET.
		 */
		
		FileDoc file = new FileDoc();
		this.CARNET.copy(file.recupCarnet("Estelle"));
		
		/*ThreadReadFile trd = new ThreadReadFile(CARNET);
		trd.start();*/		
		
		/**
		 * Initialization of the style of the frame window
		 */
		this.window.setTitle("YOUR CONTACTS");
		this.window.setSize(500, 400);
		this.window.setLocationRelativeTo(null);
				
		this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             
		this.window.setVisible(true);
		
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------		
		/**
		 * Initialization of the layout of each panel of window
		 */
		this.left.setLayout(new BorderLayout());
		this.north.setLayout(new GridLayout(2,1));
		this.right.setLayout(new BorderLayout());
		this.menuright.setLayout(new GridLayout(2,1));
		this.menuleft.setLayout(new GridLayout(2,1));
		this.searchbycategorie.setLayout(new GridLayout(1,1));
		this.showAll.setLayout(new GridLayout(1,2));

//--------------------------------------------------------------------------------------------------------------------------------------------------------		

		/**
		 * Initialization of the textArea for the function searching 
		 */
		Font police = new Font("Arial", Font.BOLD, 14);
		search.setFont(police);
		search.setPreferredSize(new Dimension(200, 30));
		search.setForeground(Color.BLUE);
		
		/**
		 * Initialization of the button "search" to lunch a search
		 */
		JButton searchButton = new JButton("by Name");
		searchButton.setBounds(75, 50, 50, 50);
		searchButton.addActionListener(new SearchAction(search, person));
			    
		
		/**
		 * Initialization of the list of contact (by default we show all contact
		 */
		list = new JList<String>(setListContact());
		list.setAlignmentY(0);
		list.setPreferredSize(new Dimension(150,300));
		list.addListSelectionListener(new ActionList(window, list, person, modify));
		list.setBackground(Color.BLUE);
		list.setForeground(Color.WHITE);
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		/**
		 * Initialization of the JtextField
		 */
		buff.append("Choose your contacts in the list");
		person.setText(buff.toString());
		person.setSize(100, 100);
				
		/**
		 * Initialization of the button "Add" a contact
		 */
		buff.setLength(0);
		buff.append("Add");
		JButton addButton = new JButton(buff.toString());
		
		addButton.setBounds(50, 60, 80, 30);
		
		buff.setLength(0);
		buff.append("To add new contact");
		addButton.setToolTipText(buff.toString());
		addButton.addActionListener(new AddMenuMain(window));
		
		/**
		 * Initialisation of modify button
		 */
		buff.setLength(0);
		buff.append("To modify a contact");
		modify.setSize(new Dimension(50,50));
		modify.setToolTipText(buff.toString());
		modify.setVisible(true);
		modify.addActionListener(new modifyAction(window,list, person));
		
		/**
		 * Initialization of the button "by group"
		 */
		buff.setLength(0);
		buff.append("by Group");
		JButton byGroup = new JButton(buff.toString());
		
		byGroup.setBounds(50, 60, 80, 30);
		
		buff.setLength(0);
		buff.append("To search by group");
		byGroup.setToolTipText(buff.toString());
		byGroup.addActionListener(new SearchByGroup(search, list ,person));
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------		
		
		/**
		 * Initialization of the button "all contact" 
		 */
		buff.setLength(0);
		buff.append("AllContact");
		JButton allButton = new JButton(buff.toString());
		allButton.setBounds(50, 60, 80, 30);
		
		buff.setLength(0);
		buff.append("To show all contact");
		allButton.setToolTipText(buff.toString());
			//	action listener of the button
		allButton.addActionListener(new ActionListener() { @Override
			public void actionPerformed(ActionEvent arg0) {
				list.removeAll();
				modify.repaint();
				list.setListData(setListContact());
				buff.setLength(0);
				buff.append("Choose your contact among all your contact");
				person.setText(buff.toString());
			} 
		}); 
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------		
		
		/**
		 * Initialization of the button "all group"
		 */
		buff.setLength(0);
		buff.append("AllGroup");
		JButton allGroup = new JButton(buff.toString());
		
		allGroup.setBounds(50, 60, 80, 30);
		buff.setLength(0);
		buff.append("To show all contact");
		allGroup.setToolTipText(buff.toString());
				//		action listener of the button
		allGroup.addActionListener(new ActionListener() { @Override
			public void actionPerformed(ActionEvent arg0) {
				list.removeAll();
				modify.repaint();
				list.setListData(setListGroup());
				buff.setLength(0);
				buff.append("Choose your group ");
				person.setText(buff.toString());
			} 
		}); 
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------		
		//set position of element
		this.searchbycategorie.add(searchButton);
		this.searchbycategorie.add(byGroup);
		
		this.showAll.add(allButton);
		this.showAll.add(allGroup);
		
		this.menuleft.add(showAll);
		this.menuleft.add(addButton);
		
		this.menuright.add(search);
		this.menuright.add(searchbycategorie);
		
		
		this.left.add(scrollPane);
		this.left.add(menuleft, BorderLayout.SOUTH);
		
		this.right.add(menuright,BorderLayout.NORTH);
		this.right.add(person,BorderLayout.CENTER);
		this.right.add(modify,BorderLayout.SOUTH);
		
		this.window.add(left,BorderLayout.WEST);
		
		this.window.add(right,BorderLayout.CENTER);
		this.window.add(north,BorderLayout.NORTH);
		
		this.window.setVisible(true);  
		
		
				
	}
//--------------------------------------------------------------------------------------------------------------------------------------------------------		

//--------------------------------------------------------------------------------------------------------------------------------------------------------		
	/**
	 * 	to set the data of the list. this method is use in the constructor of mainwindow
	 * @return String[]
	 */

	public String[] setListContact(){
		int i =0;
		
		StringBuilder tmp = new StringBuilder();
		
		String[] contentlist = new String[CARNET.getContent().size()];
		
		for(Contact con : CARNET.getContent())
        {
			tmp.append(con.getLastName()).append(" ").append(con.getFirstName());
			contentlist[i]=tmp.toString();
	        tmp.setLength(0);
	        i++;
        }
		
		return contentlist;
		
	}
	
	/**
	 * to set the content of the list with group's name. this method is use in the constructor of the main window
	 * @return String[]
	 */
	public String[] setListGroup(){
		int i =0;
		
		StringBuilder tmp = new StringBuilder();
		
		String[] contentlist = new String[CARNET.getContent().size()];
		
		for(String con : CARNET.getGroupContact())
        {
			tmp.append(con);
			contentlist[i]=tmp.toString();
	        tmp.setLength(0);
	        i++;
        }
		
		return contentlist;
		
	}
	
}
